﻿using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using IMModel.Domain.Common;

namespace IMModel.Domain
{
    partial class DepartmentTerm : IRespondentIndexVector, ICacheble
    {
        /// <summary>
        /// Получает респондентов отдела и всех его подотделов, которые являются участниками индексной системы в данном периоде
        /// </summary>
        public IEnumerable<Respondent> ParticipateRespondents
        {
            get
            {
                Contract.Requires(Respondents != null);
                Contract.Ensures(Contract.Result<IEnumerable<Respondent>>() != null);
                var respondentList = new List<Respondent>();
                MakeRespondentList(respondentList, this);
                return respondentList.AsReadOnly();                
            }
        }

        private static void MakeRespondentList(List<Respondent> respondentList, DepartmentTerm departmentTerm)
        {
            Contract.Requires(respondentList!=null);
            Contract.Requires(departmentTerm!=null);
            respondentList.AddRange(departmentTerm.Respondents.AsEnumerable().Where(x => x.IsParticipant));
            foreach (var item in departmentTerm.DepartmentTerms)
            {
                MakeRespondentList(respondentList, item);
            }
        }


        public double this[Respondent respondent]
        {
            get
            {
                Contract.Requires(Indexes!=null);
                Contract.Requires(respondent!=null);                
                return Indexes[respondent];
            }
        }
    }
}
